// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import config from "../config/config";
import func from "../config/func";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Cask extends cc.Component {

    @property({ type: cc.Node, tooltip: "啤酒机器人" })
    m_caskRobot: cc.Node = null;
    @property({ type: cc.Sprite, tooltip: "扫地机器人" })
    m_cleanRobot: cc.Sprite = null;
    @property(cc.Node)
    m_caskFrame: cc.Node[] = [];

    n_cleanRobot: cc.Animation = null;
    n_cleanRobotClips: cc.AnimationClip[] = [];
    n_caskRobot: cc.Animation = null;
    n_caskRobotClips: cc.AnimationClip[] = [];
    n_caskRobotStartPos: cc.Vec2 = cc.v2(66, 475);
    n_caskRobotEndPos: cc.Vec2 = cc.v2(0, 0);
    n_cleanRobotStartPos: cc.Vec2 = cc.v2(-100, 420);
    n_cleanRobotMiddlePos: cc.Vec2 = cc.v2();
    n_cleanRobotEndPos: cc.Vec2 = cc.v2();

    start() {
        this.m_caskRobot.setPosition(this.n_caskRobotStartPos);
        this.n_caskRobotEndPos = cc.v2(cc.winSize.width / 2 + this.m_caskRobot.width, this.n_caskRobotStartPos.y - this.m_caskRobot.height * 2);
        this.n_caskRobot = this.m_caskRobot.children[1].getComponent(cc.Animation);
        this.n_caskRobotClips = this.n_caskRobot.getClips();
        this.n_caskRobot.play();
        cc.tween(this.m_caskRobot)
            .delay(1)
            .repeatForever(
                cc.tween()
                    .to(4, { x: this.n_caskRobotEndPos.x, y: this.n_caskRobotEndPos.y })
                    .call(() => {
                        this.n_caskRobot.play(this.n_caskRobotClips[0].name);
                    })
                    .to(4, { x: this.n_caskRobotStartPos.x, y: this.n_caskRobotStartPos.y })
                    .call(() => {
                        this.n_caskRobot.play(this.n_caskRobotClips[1].name);
                    })
            )
            .start()
        this.n_cleanRobot = this.m_cleanRobot.getComponent(cc.Animation);
        this.n_cleanRobotClips = this.n_cleanRobot.getClips();
        this.m_cleanRobot.node.setPosition(this.n_cleanRobotStartPos);
        this.n_cleanRobotMiddlePos = cc.v2(-cc.winSize.width / 2 - this.m_cleanRobot.node.width * 2, this.m_cleanRobot.node.height);
        this.n_cleanRobotEndPos = cc.v2(cc.winSize.width / 2 + this.m_cleanRobot.node.width, -this.node.height / 2 + this.m_cleanRobot.node.height);
        this.n_cleanRobot.play();
        cc.tween(this.m_cleanRobot.node)
            .delay(1)
            .repeatForever(
                cc.tween()
                    .to(4, { x: this.n_cleanRobotMiddlePos.x, y: this.n_cleanRobotMiddlePos.y })
                    .call(() => {
                        this.n_cleanRobot.play(this.n_cleanRobotClips[0].name);
                    })
                    .to(8, { x: this.n_cleanRobotEndPos.x, y: this.n_cleanRobotEndPos.y })
                    .call(() => {
                        this.n_cleanRobot.play(this.n_cleanRobotClips[1].name);
                    })
                    .to(8, { x: this.n_cleanRobotMiddlePos.x, y: this.n_cleanRobotMiddlePos.y })
                    .call(() => {
                        this.n_cleanRobot.play(this.n_cleanRobotClips[0].name);
                    })
                    .to(4, { x: this.n_cleanRobotStartPos.x, y: this.n_cleanRobotStartPos.y })
                    .call(() => {
                        this.n_cleanRobot.play(this.n_cleanRobotClips[1].name);
                    })
            )
            .start();
        this.schedule(this.showTip, 15);
    }

    showTip() {
        let ran = Math.floor(Math.random() * 4),
            tipFrame = this.m_caskFrame[ran],
            text = tipFrame.children[0].getComponent(cc.Label),
            ranKey = Math.floor(Math.random() * 5) + 10001,
            dialogue = config.dataJson.dialogue[ranKey];
        tipFrame.active = true;
        func.setNodeAni(tipFrame);
        setTimeout(() => {
            if(this.node && this.node.active){
                func.goReduceOut(tipFrame);
            }
        }, 4000);
        switch (ran) {
            case 0:
                text.string = dialogue.littleRobot
                break;
            case 1:
                text.string = dialogue.sweeping;
                break;
            case 2:
                text.string = dialogue.cook;
                break;
            case 3:
                text.string = dialogue.cook2;
                break;
        }
    }
    onDisable() {
        this.unschedule(this.showTip);
    }
}
